package com.goods.controller.business;

import com.goods.business.service.CategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "业务管理-物资类别相关接口")
@RequestMapping("/business/productCategory")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //分页展示分类数据
    @GetMapping("/categoryTree")
    public ResponseBean showCategory(@RequestParam Map pageMap) {
        Map resultMap = new HashMap();
        if (pageMap!=null && pageMap.size()!=0) {
            resultMap = categoryService.showCategory(pageMap);
        } else {
            resultMap = categoryService.showCategory();
        }
        return ResponseBean.success(resultMap);
    }

    //展示父分类
    @GetMapping("/getParentCategoryTree")
    public ResponseBean showFatherCategory() {
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOList = categoryService.showFatherCategory();
        return ResponseBean.success(productCategoryTreeNodeVOList);
    }

    //添加
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Map addForm) {
        categoryService.addCategory(addForm);
        return ResponseBean.success();
    }

    //编辑弹窗数据回显
    @GetMapping("/edit/{id}")
    public ResponseBean showEdit(@PathVariable Long id) {
        ProductCategoryVO productCategoryVO = categoryService.showEdit(id);
        return ResponseBean.success(productCategoryVO);
    }

    //修改
    @PutMapping("/update/{id}")
    public ResponseBean update(@PathVariable Long id,@RequestBody ProductCategoryVO productCategoryVO) {
        categoryService.update(id,productCategoryVO);
        return ResponseBean.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        categoryService.delete(id);
        return ResponseBean.success();
    }
}
